package com.prs.crm.dao.project.impl;

import java.util.Collection;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.project.ProjectDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.project.Project;
import com.prs.crm.domain.project.Task;

public class ProjectDaoImpl extends BaseDaoImpl<Project> implements ProjectDao {

	@Override
	public Collection<?> getProjectReportDataSource(Integer state,
			Date datestart, Date dateend, Collection<Person> persons) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(Task.class);
		if(state != null){
			criteria.add(Restrictions.eq("status", state));
		}
		if(datestart != null){
			criteria.add(Restrictions.ge("startTime", datestart));
		}
		if(dateend != null){
			criteria.add(Restrictions.le("startTime", dateend));
		}
		if(persons != null){
			criteria.add(Restrictions.in("executor", persons));
		}
		ProjectionList projectionlist=Projections.projectionList();  
    	projectionlist.add(Projections.groupProperty("executor"));
    	projectionlist.add(Projections.rowCount());
    	criteria.setProjection(projectionlist);
		return criteria.list();
	}

	
}
